package com.zhu.esyblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.esyblog.Utils.BeanCopyUtils;
import com.zhu.esyblog.pojo.Category;
import com.zhu.esyblog.mapper.CategoryMapper;
import com.zhu.esyblog.pojo.RespBean;
import com.zhu.esyblog.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.esyblog.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2022-06-22
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 根据id查询分类标签
     * @param categoryId
     * @return
     */
    @Override
    public CategoryVo findCateGoryById(Integer categoryId) {
        Category category = categoryMapper.selectById(categoryId);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return categoryVo;
    }

    /**
     * 获取所有标签
     * @return
     */
    @Override
    public RespBean findAll() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Category::getId,Category::getCategoryName);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        List<CategoryVo> categoryVoList = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return RespBean.success("success",categoryVoList);
    }

    @Override
    public RespBean categoriesDetail() {
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<>());
        List<CategoryVo> categoryVoList = BeanCopyUtils.copyBeanList(categoryList, CategoryVo.class);
        return RespBean.success("success",categoryVoList);

    }

    /**
     * 根据文章分类查询文章
     * @param id
     * @return
     */
    @Override
    public RespBean categoriesDetailById(Long id) {
        Category category=categoryMapper.selectById(id);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return RespBean.success("success",categoryVo);
    }
}
